package com.guangyunl.e_recursion;

import java.util.Scanner;

// 计算斐波那契数列(Fibonacci)的第n个值
// 规律：一个数等于前两个数之和，比如：1  1  2  3  5  8  13  21  34  55  .…
public class Demo04Recursion {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入要求的是第几个值: ");
        int a = scanner.nextInt();

        int fibonacci = fibonacci(a);
        System.out.println("fibonacci("+ a + ") = " + fibonacci);
    }

    public static int fibonacci(int n){
        if(n == 1 || n == 2){
            return 1;
        }else{
            return fibonacci(n-1)+fibonacci(n-2);
        }

    }
}
/*
请输入要求的是第几个值: 10
fibonacci(10) = 55
 */